import {ObjectDto} from "./object.dto";
import {TableInterface} from "./sqlite.service";


export class VerifyService {
    constructor() {
    //
    }

    verifyColumn <T extends ObjectDto>(model: T, keyArray: Array<string>) {
        let table = model.table();
        let obj = {};
        if (keyArray.length === 0 ) {
          for (let key of Object.keys(table.tableConstructor)) {
              obj[key] = model[key];
          }
          return obj;
        }
        else {
          keyArray.forEach((item, index) => {
            if (!table.tableConstructor.hasOwnProperty(`${item}`)) {
              console.error(`this table no exist ${item} column`);
            } else {
              // console.log(model);
              obj[item] = model[item];
            }
          });
        }
        return obj;
    }

    verifyType(constructor: TableInterface , obj: object = {}): object {
        let newObj = {};
        for (let key of Object.keys(obj)) {
            let flag = "";
            for (let k of Object.keys(constructor.tableConstructor)) {
                if (key === k) {
                    let regText = new RegExp("text");
                    let regboolean = new RegExp("boolean");
                    if (regText.test(constructor.tableConstructor[k]) || regboolean.test(constructor.tableConstructor[k])) {
                        flag = "text";
                        break;
                    }

                    let regUnique = new RegExp("unique");
                    if (regUnique.test(constructor.tableConstructor[k])) {

                        flag = "unique";
                        break;
                    }

                    let regAutoincrement = new RegExp("autoincrement");
                    if (regUnique.test(constructor.tableConstructor[k])) {
                        flag = "regAutoincrement";
                        break;
                    }
                }
            }
            // if (flag === "text") {
            if (flag === "text") {
                // console.log((obj[key] !== undefined || obj[key] !== null || obj[key] !== "null"), obj[key]);
                newObj[key] = obj[key] || obj[key] === 0 || obj[key] === false ? `"${obj[key]}"` : null;
            }
            if (flag === "" || flag === "unique") {
                newObj[key] = obj[key];
            }
        }
        return newObj;
    }


}
